<?php
class DepotManagementModel extends SBKH{		
	
	function getDistrictRecords($pro_id = 0, $dis_id = 0,$search = "", $start = 0, $end = 15){
		$sql = "
		SELECT de.*,
				(SELECT `district_name` FROM " . $this -> GetPrefix('district') . " as d WHERE d.`id` = de.`district_id`) as district_name 
		FROM " . $this -> GetPrefix('depot') . " AS de , " . $this -> GetPrefix('district') . " AS d2
		";
		$where = "";
		if($dis_id != 0 && $search == ""){
			$where .= " WHERE de.`district_id` = '" . $dis_id . "'";
		}elseif($dis_id == 0 && $search != ""){
			$where .= " WHERE de.`depot_name` LIKE '%" . $search . "%'";
		}elseif($dis_id != 0 && $search != ""){
			$where .= " WHERE de.`district_id` = '" . $dis_id . "' && de.`depot_name`= '%" . $search . "%'";
		}
		/*if($pro_id != 0){
			if($where != ""){
				$where .= " AND d2.`province_id` = '" . $pro_id . "'";
			}else{
				$where .= " WHERE d2.`id` = '" . $pro_id . "'";
			}
		}*/
		
		$sql .= $where;
		
		if($end == 0){
			$sql .= " GROUP BY de.id ORDER BY  de.`district_id` DESC";
		}else{
			$sql .= " GROUP BY de.id ORDER BY  de.`district_id` DESC LIMIT $start,$end";
		}			
		$result = $this->query($sql);
		/* For Pagination */
		$sql = "SELECT COUNT(de.`id`) as number_of_rows FROM " . $this -> GetPrefix('depot') . " as de";
		$sql .= $where;
		$result2 = $this->query($sql);
		return array($result,$result2[0]->number_of_rows);
	}	
	
	function getRecordByID($depot_id){
		$sql = "SELECT de.*, p.`province_name`, p.`id` as province_id
				FROM " . $this -> GetPrefix('depot') . " AS de 
				INNER JOIN " . $this -> GetPrefix('district') . " AS di ON di.`id` = de.`district_id`
				INNER JOIN " . $this -> GetPrefix('province') . " AS p ON p.`id` = di.`province_id`  
				WHERE de.`id` = '" . $depot_id . "'";
		$result = $this -> Query($sql);
		return $result;
	}
	function getDistrictByProvinceID($pro_id){
		$sql = "SELECT di.*
				FROM " . $this -> GetPrefix('district') . " AS di				
				WHERE di.`province_id` = '" . $pro_id . "'";
		$result = $this -> Query($sql);
		return $result;
	}
	function delete($depot_id = 0){
		$sql = "
		DELETE FROM " . $this -> GetPrefix('depot') . "
		WHERE `id` = '" . $depot_id . "'
		";
		return $this->query($sql);
	}
}
?>